package com.example.bledemo;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;

public class BleAdapter extends BaseAdapter {

    private final List<BluetoothDevice> mDeviceList;
    private final List<Integer> mRssi;
    private final List<byte[]> mScanRecord;
    private final LayoutInflater mInflater;

    public BleAdapter(List<BluetoothDevice> deviceList,List<Integer> rssi, List<byte[]> scanRecord, Context context){
        this.mDeviceList=deviceList;
        this.mRssi=rssi;
        this.mScanRecord=scanRecord;
        this.mInflater=LayoutInflater.from(context);
    }

    //scanRecords的格式转换
    static final char[] hexArray = "0123456789ABCDEF".toCharArray();
    private static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte aByte : bytes) {
            hexString.append(String.format("%02X", aByte)).append(' ');
        }
        return hexString.toString();
    }


    @Override
    public int getCount() {
        return mDeviceList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDeviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }



    @SuppressLint({"SetTextI18n", "InflateParams"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHandler viewHandler;
        if(convertView==null){
            viewHandler=new ViewHandler();
            convertView=mInflater.inflate(R.layout.list_ble_device,null);
            viewHandler.ble_device_name=(TextView) convertView.findViewById(R.id.ble_device_name);
            viewHandler.ble_device_address=(TextView) convertView.findViewById(R.id.ble_device_address);
            viewHandler.ble_device_rssi=(TextView) convertView.findViewById(R.id.ble_device_rssi);
            viewHandler.ble_device_scanRecord=(TextView) convertView.findViewById(R.id.ble_device_scanRecord);
            viewHandler.ble_device_connect_state=(TextView) convertView.findViewById(R.id.ble_device_connect_state);
            convertView.setTag(viewHandler);
        }else{
            viewHandler=(ViewHandler)convertView.getTag();
        }

        if(mDeviceList.get(position).getName() == null)
            viewHandler.ble_device_name.setText("name: "+"unknow");
        else viewHandler.ble_device_name.setText("name: "+mDeviceList.get(position).getName());
        viewHandler.ble_device_address.setText("mac: "+mDeviceList.get(position).getAddress());
        viewHandler.ble_device_rssi.setText("rssi: "+Integer.toString(this.mRssi.get(position)));
        viewHandler.ble_device_scanRecord.setText("scanRecord: "+ bytesToHex(this.mScanRecord.get(position)));

        if(this.mScanRecord.get(position)[1] == 0x01) {
            viewHandler.ble_device_connect_state.setText("state: " + "可连接");
        }
        else viewHandler.ble_device_connect_state.setText("state: " + "不可连接");
        return convertView;
    }

    static class  ViewHandler{
        private TextView ble_device_name;
        private TextView ble_device_address;
        private TextView ble_device_rssi;
        private TextView ble_device_scanRecord;
        private TextView ble_device_connect_state;
    }
}

